package com.pai4j.client.controller;
import com.pai4j.client.service.CommentService;
import com.pai4j.common.bean.PAIPageResponseBeanUtil;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.config.annotation.SafeClick;
import com.pai4j.common.enums.ClickEvent;
import com.pai4j.common.helper.SessionHelper;
import com.pai4j.domain.vo.request.request.CommentSaveVO;
import com.pai4j.domain.vo.response.CommentResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@RestController
@RequestMapping("/client-service/comment")
public class CommentController {


    @Autowired
    private CommentService commentService;

    /**
     * 发布动态
     *
     * @return
     */
    @SafeClick(event = ClickEvent.COMMENT, time = 2000, message = "评论发布中，请勿重复操作")
    @PostMapping("/addComment")
    public PAIResponseBean<Long> saveComment(@RequestBody CommentSaveVO vo) {
        vo.setIp(SessionHelper.getCurrentClientIP());
        Long commentId = commentService.saveComment(vo);
        return PAIResponseBeanUtil.success(commentId);
    }

    /**
     * 删除评论
     *
     * @return
     */
    @PostMapping("/deleteComment/{id}")
    public PAIResponseBean deleteComment(@PathVariable Long id) {
        String userId = SessionHelper.getCurrentUserId();
        commentService.deleteComment(id, userId);
        return PAIResponseBeanUtil.success();
    }




    /***
     * 查询资源评论列表
     * @param resourceId
     * @param resourceType
     * @param isHot
     * @param isNew
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/query")
    public PAIPageResponseBeanUtil<List<CommentResponseVO>> queryComments(@RequestParam(value = "resourceId", required = false) Long resourceId,
                                                                          @RequestParam(value = "resourceType", required = false) String resourceType,
                                                                          @RequestParam(value = "content", required = false) String content,
                                                                          @RequestParam(value = "isHot", required = false, defaultValue = "false") Boolean isHot,
                                                                          @RequestParam(value = "isNew", required = false, defaultValue = "false") Boolean isNew,
                                                                          @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                          @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

       return commentService.queryCommentByCondition(content, resourceId, resourceType, pageNo, pageSize, isHot, isNew);
    }


}
